﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	FormsListFieldIterator.cs
//  Developer:	Solomatov Igor
//  Created:	03.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Smart.Common.Entities;
using Smart.Common.Forms;

namespace Smart.Common.WebControls
{
	/// <summary>
	/// Field iterator via Forms service
	/// </summary>
	public class FormsListFieldIterator : ListFieldIterator
	{
		/// <summary>
		/// Scope name
		/// </summary>
		[SPWebCategoryName("Settings")]
		[Localizable(true)]
		[WebBrowsable(true)]
		[WebPartStorage(Storage.Personal)]
		[Personalizable(PersonalizationScope.Shared)]
		[WebDisplayName("Scope name")]
		[Description("")]
		[RefreshProperties(RefreshProperties.All)]
		public string ScopeName { get; set; }

		/// <summary>
		/// Forms service name
		/// </summary>
		[SPWebCategoryName("Settings")]
		[Localizable(true)]
		[WebBrowsable(true)]
		[WebPartStorage(Storage.Personal)]
		[Personalizable(PersonalizationScope.Shared)]
		[WebDisplayName("Forms service name")]
		[Description("")]
		[RefreshProperties(RefreshProperties.All)]
		public string FormsServiceName { get; set; }

		/// <summary>
		/// Create child controls using Forms factory
		/// </summary>
		protected override void CreateChildControls()
		{
			bool formBuilded;

			try
			{
				if (DesignMode)
					return;

				if (List == null)
					return;

				var mode = ControlMode.ToString().SToE<MetaFormMode>();
				var entity = (ListItem != null ? ListItem.ToEntity() : null);
				var ctid = string.Empty;

				if (mode == MetaFormMode.New && List.ContentTypes != null && List.ContentTypes.Count > 0)
				{
					ctid = Context.Request.QueryString["ContentTypeId"];

					ctid = (ctid.IsNullOrEmpty() ? List.ContentTypes[0] : List.ContentTypes[new SPContentTypeId(ctid)]).Parent.Id.ToString();
				}
				else if (entity != null && (mode == MetaFormMode.Edit || mode == MetaFormMode.Display))
				{
					ctid = entity.ContentTypeId.ToString();
				}

				formBuilded =
					SPServices.FormsFactory.BuildForm(
						GetFormsServiceName(),
						new FormEntityGeneric((entity == null ? Identifier.Empty : entity.ID), mode == MetaFormMode.New, entity) {ContentTypeId = ctid},
						new FormContainerGeneric(Controls),
						mode,
						null);
			}
			catch (Exception x)
			{
				H.LX("FormsListFieldIterator", x);

				formBuilded = false;
			}

			if (!formBuilded)
			{
				Controls.Clear();

				base.CreateChildControls();
			}
		}

		/// <summary>
		/// Return forms service name
		/// </summary>
		protected virtual string GetFormsServiceName()
		{
			return
				FormsServiceName.IsNullOrEmpty()
					? SPServices.Configuration.GetValue(
						Const.Forms.WebForms.DefaultFormsServiceConfigKey,
						Const.Forms.WebForms.ListFormsServiceName)
					: FormsServiceName;
		}
	}
}